import {
	requestPermissionall,
	requestPermission,
	requestPermissionasync,
} from '@/js_sdk/wa-permission/permission.js'
const utils = {
	getAuthorizeInfo(a = "scope.userLocation") { //1. uniapp弹窗弹出获取授权（地理，个人微信信息等授权信息）弹窗
		var _this = this;
		console.log('getAuthorizeInfo ing');
		return new Promise((resolve, reject) => {
			if (uni.getStorageSync('latLng')) {
				resolve(uni.getStorageSync('latLng'))
			} else {
				uni.authorize({
					scope: a,
					success() { //1.1 允许授权
						uni.getLocation({
							// type: 'wgs84',
							type: 'gcj02',
							success(res) {
								console.log("你当前经纬度是：", res);
								console.log(res)
								let latitude, longitude;
								latitude = res.latitude.toString();
								longitude = res.longitude.toString();
								uni.setStorageSync('latLng', [latitude, longitude])
								resolve([latitude, longitude])
							}
						});
					},
					fail() { //1.2 拒绝授权
						console.log("你拒绝了授权，无法获得地区信息")

						uni.showToast({
							title: '你拒绝了授权，无法获得地区信息',
							icon: 'none'
						})
						console.log("wwwwwwwwwwwwwwwwwwwwwwwww", uni.getStorageSync('latLng')[0],
							uni.getStorageSync('latLng')[1]);
						// this.$api.post({
						// 	url: '/wanlshop.common/get_current_city',
						// 	data: {
						// 		lat: +uni.getStorageSync('latLng')[0],
						// 		lng: +uni.getStorageSync('latLng')[1]
						// 	},

						// 	success: res => {
						// 		console.log('city res', res);
						// 		if (res.address) {
						// 			this.currentCity = res.address
						// 		}
						// 	}
						// });
						reject()
					}
				})
			}
		})
	},

	getAuthorizeInfoMap(a = "scope.userLocation", latitude = "", longitude =
		"") { //1. uniapp弹窗弹出获取授权（地理，个人微信信息等授权信息）弹窗
		var _this = this;
		return new Promise((resolve, reject) => {
			uni.authorize({
				scope: a,
				success() { //1.1 允许授权
					uni.chooseLocation({
						// type: 'wgs84',
						type: 'gcj02',
						latitude: latitude,
						longitude: longitude,
						success(res) {
							console.log("你当前经纬度是：", res);
							console.log(res)
							let latitude, longitude;
							latitude = res.latitude.toString();
							longitude = res.longitude.toString();
							uni.setStorageSync('latLng', [latitude, longitude])
							resolve(res)
						},
						fail(err) {
							console.log(err)
						}
					});
				},
				fail() { //1.2 拒绝授权
					console.log("你拒绝了授权，无法获得地区信息")
					uni.showToast({
						title: '您拒绝了授权，无法获得地区信息',
						icon: 'none'
					})
					reject()
				}
			})
		})
	},
	getAuthorizeInfoMapForApp(a = "scope.userLocation", latitude = "", longitude =
		"") { //1. uniapp弹窗弹出获取授权（地理，个人微信信息等授权信息）弹窗
		var _this = this;
		return new Promise((resolve, reject) => {
			uni.chooseLocation({
				// type: 'wgs84',
				type: 'gcj02',
				latitude: latitude,
				longitude: longitude,
				success(res) {
					console.log("你当前经纬度是：", res);
					console.log(res)
					let latitude, longitude;
					latitude = res.latitude.toString();
					longitude = res.longitude.toString();
					uni.setStorageSync('latLng', [latitude, longitude])
					resolve(res)
				},
				fail(err) {
					console.log(err + "无法获得地区信息")
					uni.showToast({
						title: '您拒绝了授权，无法获得地区信息',
						icon: 'none'
					})
					reject()
				}
			});
		})
	},
	prePage() {
		let pages = getCurrentPages();
		let prePage = pages[pages.length - 2];
		// #ifdef H5
		return prePage;
		// #endif
		return prePage.$vm;
	},
	serilizeURL(url) {
		url = decodeURIComponent(url)
		var rs = url.split("?")[1];
		if (!rs) {
			rs = url
		}
		rs = rs.replace('scene=', '')
		var arr = rs.split("&");
		var json = {};
		for (var i = 0; i < arr.length; i++) {
			if (arr[i].indexOf("=") != -1) {
				json[arr[i].split("=")[0]] = arr[i].split("=")[1];
			} else {
				json[arr[i]] = "undefined";
			}
		}
		return json
	}, //广告页面跳转
	imageclicks(advertising) {
		console.log("464648", advertising);
		if (advertising.ad_type == 'url') {
			uni.navigateTo({
				url: '/pages/web/web?url=' + encodeURIComponent(advertising.ad_val)
			})
		}
		if (advertising.ad_type == 'miniprogram') {
			uni.navigateTo({
				url: advertising.ad_val,
				fail() {
					uni.switchTab({
						url: advertising.ad_val
					})
				}
			})
		}
		if (advertising.ad_type == 'minishop') {
			uni.navigateTo({
				url: '/pages/shangjia/shangjia?storeid=' + advertising.ad_val
			})
		}
	},
	getLocation(force = false) {
		return new Promise((resolve, reject) => {
			let now = Number(new Date)
			let huancun = uni.getStorageSync('latLng')
			console.log("333333333333333", now - huancun[2]);
			if (!force && huancun && (!huancun[2] || (now - huancun[2]) < 60000)) {
				resolve(huancun)
			} else {
				console.log('777777777777777');
				requestPermissionasync('location').then(value => {
					console.log('定位结果', value);
					//权限没有通过会false并请求授权弹窗或授权设置页
					//通过后直接执行
					if (value) {
						//您的业务代码
						uni.getLocation({
							// type: 'wgs84',
							type: 'gcj02',
							success(res) {
								console.log("你当前经纬度是：", res);
								console.log(res)
								let latitude, longitude;
								latitude = res.latitude.toString();
								longitude = res.longitude.toString();
								uni.setStorageSync('latLng', [latitude, longitude, Number(
									new Date)])
								resolve([latitude, longitude])
							},
							fail(err) {
								console.log("getLocation fail", err)
								reject(err)
							}
						});
					} else {
						reject(-2)
					}
				}).catch((s) => {
					console.log('fsdfas ssssssssssss', s);
					reject(s)
				})
			}
		})
	},
}
export default utils